package com.bocai.dahuatech.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.AddressListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.ui.pressent.AddressIdCallBack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PointExchangeAddrAdapter extends BaseAdapter {
    private Context context;
    List<AddressListBean.ContentEntity> datas;
    public Map<Integer, Boolean> selectedMap;
    AddressIdCallBack callBack;
    public PointExchangeAddrAdapter(Context context,List<AddressListBean.ContentEntity> datas,AddressIdCallBack callBack){
        this.context=context;
        this.datas=datas;
        this.callBack=callBack;
        // 保存每条记录是否被选中的状态
        selectedMap = new HashMap<>();
        for (int i = 0; i < datas.size(); i++)
        {
            if(i==0)
            {
                selectedMap.put(i, true);
            }else
            {
                selectedMap.put(i, false);
            }
        }
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null)
        {
            holder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.pop_point_exchange_addr_item,null);
            holder.cbChoose= (CheckBox) convertView.findViewById(R.id.cb_pointpop_choose);
            holder.tvAddr=(TextView)convertView.findViewById(R.id.tv_pointpop_addr);
            holder.cbChoose.setOnClickListener(new MyOnClickListener(position));
            convertView.setTag(holder);
        }else
        {
            holder= (ViewHolder) convertView.getTag();
        }
        //收货人：张哲伦 收货地址：杭州市西湖区 电话：123456789
        holder.tvAddr.setText(context.getResources().getString(R.string.consignee)+datas.get(position).getFConsignee()+context.getResources().getString(R.string.receiptaddress)+datas.get(position).getPCAName()+datas.get(position).getFAddress()
                +context.getResources().getString(R.string.receipttel)+datas.get(position).getFMobilePhone());
        holder.cbChoose.setChecked(selectedMap.get(position));
        return convertView;
    }

    class  MyOnClickListener implements View.OnClickListener{
        int position;
        public MyOnClickListener(int position)
        {
            this.position=position;
        }

        @Override
        public void onClick(View v) {
            for (int i=0;i<selectedMap.size();i++){
                selectedMap.put(i,false);
            }
            selectedMap.put(position, true);
            notifyDataSetChanged();
            callBack.getAddrId(datas.get(position).getFID());

        }
    }
    class ViewHolder{
        CheckBox cbChoose;
        TextView tvAddr;

    }
}
